﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_Building
    {
        public bool CreateOneBuilding(Building_Info objBuilding_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Building_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"BuildingID", objBuilding_Info.BuildingID));
            command.Parameters.Add(new SqlParameter(@"FieldLocationID", objBuilding_Info.FieldLocationID));
            command.Parameters.Add(new SqlParameter(@"BuildingName", objBuilding_Info.BuildingName));
            command.Parameters.Add(new SqlParameter(@"Description", objBuilding_Info.Description));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneBuilding(Building_Info objBuilding_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Building_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@BuildingID", objBuilding_Info.BuildingID));
            command.Parameters.Add(new SqlParameter("@FieldLocationID", objBuilding_Info.FieldLocationID));
            command.Parameters.Add(new SqlParameter("@BuildingName", objBuilding_Info.BuildingName));
            command.Parameters.Add(new SqlParameter("@Description", objBuilding_Info.Description));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneBuilding(int BuildingID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Building_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"BuildingID", BuildingID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllBuilding()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Building_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public Building_Info GetOneBuilding(int BuildingID)
        {
            Building_Info objBuilding_Info = new Building_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("Building_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"BuildingID", BuildingID));
            try
            {
                objConnection.Open();
                SqlDataReader ordBuilding = command.ExecuteReader(); //OjectRecordDatareader
                ordBuilding.Read();
                if (ordBuilding.HasRows)
                {
                    if (ordBuilding["BuildingID"] != DBNull.Value)
                    {
                        objBuilding_Info.BuildingID = Convert.ToInt16(ordBuilding["BuildingID"]);
                    }
                    if (ordBuilding["FieldLocationID"] != DBNull.Value)
                    {
                        objBuilding_Info.FieldLocationID = Convert.ToInt16(ordBuilding["FieldLocationID"]);
                    }
                    if (ordBuilding["BuildingName"] != DBNull.Value)
                    {
                        objBuilding_Info.BuildingName = Convert.ToString(ordBuilding["BuildingName"]);
                    }
                    if (ordBuilding["Description"] != DBNull.Value)
                    {
                        objBuilding_Info.Description = Convert.ToString(ordBuilding["Description"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objBuilding_Info;
        }
    }
}
